# -*- coding: utf-8 -*-
__author__ = 'genghz'

import json
import pika


class Sender:

    def __init__(self, url):
        parameters = pika.URLParameters(url)
        self.connection = pika.BlockingConnection(parameters)
        self.channel = self.connection.channel()
        self.on_open()

    def push(self, exchange, routin_key, message, type_id=''):
        properties = pika.BasicProperties(content_type='application/json',
                                          headers={'__TypeId__': type_id})
        self.channel.basic_publish(exchange,
                                   routin_key,
                                   json.dumps(message),
                                   properties)

    def close(self):
        self.channel.close()
        self.connection.close()

    def on_open(self):
        print 'connect success ...' if self.connection.is_open else 'connect fail ...'

if __name__ == '__main__':
    URL = 'amqp://guest:guest@127.0.0.1:5672/%2F/test'

    sender = Sender(URL)

    message = {
        'taskId': '5858c7a39e3626600232db99',
        'bankCardId': 11
    }
    print json.dumps(message)
    sender.push('task-exchange', 'task', message,
                'com.kuaihaowei.models.task.excel.UnapplyExcelTask')

    sender.close()
